import datetime
import json
import os
import re
import sys

import requests

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from aws_model.athena_submit import submit

tables = {'ods.kyd_ods': 's3://ods/kyd_ods',
          'ods.iot_ac': 's3://ods/iot_ac',
          'ods.iot_ac_mqtt': 's3://ods/iot_ac_mqtt',
          'ods.iot_ac_tc': 's3://ods/iot_ac_tc',
          'ods.iot_fridge': 's3://ods/iot_fridge',
          'ods.iot_fridge_mqtt': 's3://ods/iot_fridge_mqtt',
          'ods.iot_washer': 's3://ods/iot_washer',
          'original.iot_hbase_prod': 's3://hmsj-bigdata/hbase-prod'}


def readable_file_size(bytes, precision):
    for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']:
        if abs(bytes) < 1024.0:
            return '%s %s' % (format(bytes, '.%df' % precision), unit)
        bytes /= 1024.0
    return '%s %s' % (format(bytes, '.%df' % precision), 'Yi')


def data_size(directory, date):
    try:
        p = os.popen(f'aws s3 ls --summarize --recursive {directory}/p_date={date}')
        result = p.read()
        size = re.findall(r"Total Size: (\d+)", result)[0]
        p.close()
        return size
    except Exception as e:
        return '0'


def run():
    count_map = {}
    date = (datetime.datetime.today() + datetime.timedelta(hours=8) - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

    for table, s3_location in tables.items():

        if 'kyd' in table:
            query_date = ((datetime.datetime.today() + datetime.timedelta(hours=8) - datetime.timedelta(
                days=2)).strftime('%Y-%m-%d'))[0:7]
        else:
            query_date = date
        sql = f'''select count(1) from {table} where p_date = '{query_date}' '''
        print(sql)
        rs = submit(sql)
        size = data_size(s3_location, query_date)
        try:
            count_map[table] = [rs['result'][0][0], readable_file_size(int(size), 2)]
        except Exception as e:
            count_map[table] = 'SQL ERROR'
    send(count_map, date)


def send(map, date):
    qywx_robot_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
    token = '0c4a703b-fa8b-4348-a442-9c690a605c60'
    send_url = qywx_robot_url + token
    headers = {
        'Content-Type': 'application/json'
    }
    try:
        content = f'<font color=\"warning\">Athena数据统计-{date}</font>'
        for k, v in map.items():
            content = content + f"\n> {k}: <font color=\"comment\"> {v[0]} ({v[1]}) </font> "
        msg = {
            "msgtype": 'markdown',
            "markdown": {'content': content}
        }
    except Exception as e:
        msg = {
            "msgtype": 'text',
            "text": {'content': f'执行错误,{str(map)}'}
        }
    response = requests.post(url=send_url, data=json.dumps(msg), headers=headers)
    print(map)
    print('已发送消息到企业微信')


if __name__ == '__main__':
    run()
